<?php

class Database
{
    var $database_name;
    var $database_user;
    var $database_pass;
    var $database_host;
    var $database_link;

    function  __construct()
    {
        $this->loadProperties();
    }

    function connect()
    {
        $this->database_link = mysql_connect($this->database_host, $this->database_user, $this->database_pass) or die("Could not make connection to MySQL");
        mysql_select_db($this->database_name) or die ("Could not open database: ". $this->database_name);
    }

    function disconnect()
    {
        if(isset($this->database_link))
        {
            mysql_close($this->database_link);
        }
        else
        {
            mysql_close();
        }
    }

    function getResults($query)
    {
        if(!isset($this->database_link))
        {
            $this->connect();
        }

        $result = mysql_query($query, $this->database_link) or die("Error: ". mysql_error());

        $returnArray = array();

        while ($row = mysql_fetch_array($result, MYSQL_BOTH))
        {
            if ($row)
            {
                $returnArray[] = $row;
            }
        }

        mysql_free_result($result);

        return $returnArray;
    }

    function getResultsWithoutLimit($query)
    {
        if(!isset($this->database_link))
        {
            $this->connect();
        }

        $limit_pos = strpos($query, "LIMIT");

        if($limit_pos != FALSE)
        {
            $query = substr($query, 0, $limit_pos);
        }

        $result = mysql_query($query, $this->database_link)  or die("Error: ". mysql_error());

        $resultArray = array();

        while ($row = mysql_fetch_array($result, MYSQL_BOTH))
        {
            if ($row)
            {
                $resultArray[] = $row;
            }
        }

        return $resultArray;
    }

    function runQuery($query)
    {
        if(!isset($this->database_link))
        {
            $this->connect();
        }

        $result = mysql_query($query, $this->database_link) or die("Error: ". mysql_error());

        if($result)
        {
            return $result;
        }
    }

    public function getRecordsCount($table)
    {
        $query = "SELECT COUNT(*) AS num FROM " . $table;

        $total_records = mysql_fetch_array(mysql_query($query));

	$total_records = $total_records["num"];

        return $total_records;
    }

    private function loadProperties()
    {
        if (file_exists("includes/custom"))
        {
            $filePath="includes/custom/";
        }
        else if (file_exists("../includes/custom"))
        {
            $filePath="../includes/custom/";
        }

        $fp = fopen($filePath."db.properties", "r") or die ("Could not open ".$filePath."db.properties file");

        // iterate through all the lines
	while ($line = fgets($fp))
	{
            // getting the value of the property
            $valueIndex = stripos($line, "=");
            $value = substr($line, $valueIndex +1);

            
            if (strpos($line, "Host") !== false)
            {
                // if the current line contains 'server' then this value is for 'host'
		$this->database_host = trim($value);
            }
            elseif (strpos($line, "Username") !== false)
            {
                // if the current line contains 'username' then this value is for 'username'
		$this->database_user = trim($value);
            }
            elseif (strpos($line, "Password") !== false)
            {
                // if the current line contains 'password' then this value is for 'password'
		$this->database_pass = trim($value);
            }
            elseif (stripos($line, "Database") !== false)
            {
                // if the current line contains 'database' then this value is for 'database'
		$this->database_name = trim($value);
            }
       	}

	fclose($fp);
    }
}

?>
